using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using StickyNotesPrinter.Core;
using StickyNotesPrinter.Specs.FogBugz;

namespace FogBugz.Bridge
{
    public class Api : ITrackerApi
    {
        private readonly ApiSettings _apiSettings;
        private string _token = string.Empty;

        public Api(ApiSettings apiSettings)
        {
            _apiSettings = apiSettings;
            LogIn();
        }

        
	    private void LogIn()
	    {
            if(_token.Length > 0) return;
	        var xml = CallApi("cmd=logon&email=" + _apiSettings.Username + "&password=" + _apiSettings.Password);
	        try
	        {
	            _token = xml.GetElementAs<String>("token");
	        }
	        catch (Exception ex)
	        {
	            ex.Source = xml.ToString();
	            throw;
	        }
	    }

	    private XElement CallApi(string parameters)
	    {
	        if (_token.Length > 0) return XElement.Load(_apiSettings.EndPointBase + "?token=" + _token + "&" + parameters);
	        return XElement.Load(_apiSettings.EndPointBase + "?" + parameters);
	    }

        public IEnumerable<Iteration> GetIterations()
        {
            var xml = CallApi("cmd=listFixFors");
            var iterations = xml.Element("fixfors").Elements("fixfor").Select(ff => new Iteration
                                                                     {
                                                                         Id = ff.GetElementAs<Int16>("ixFixFor"),
                                                                         Name = ff.GetElementAs<String>("sFixFor")
                                                                     });
            return iterations;
        }

        public IEnumerable<Story> GetStoriesForIteration(Iteration iteration)
        {
            var xml = CallApi(string.Format("cmd=search&q=fixfor:{0}&cols=ixBugParent,ixBugChildren,Tags,sCategory,sProject,sTitle,sLatestTextSummary,hrsCurrEst", iteration.Name));
            var stories = xml.Element("cases").Elements("case").Select(ff => new Story
                                                                                 {
                                                                                     Id = ff.GetAttrributeAs<Int16>("ixBug"),
                                                                                     Title = ff.GetElementAs<String>("sTitle"),
                                                                                     Parent = ff.GetElementAs<Int16>("ixBugParent"),
                                                                                     Childrens = ff.GetElementAs<string>("ixBugChildren").Split(','),
                                                                                     Category = ff.GetElementAs<String>("sCategory"),
                                                                                     Project = ff.GetElementAs<String>("sProject"),
                                                                                     Description = ff.GetElementAs<String>("sLatestTextSummary"),
                                                                                     Points = ff.GetElementAs<int>("hrsCurrEst") / 2
                                                                                 });
            return stories;
        }
    }
}